import 'package:flutter/material.dart';

import 'package:flutter_widgets_pc/widgets/button/kq_underline_button.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';
import 'entity/kq_form_entity.dart';

//展示附件信息
class KqAttachmentView extends StatefulWidget {
  /// 通用属性
  final KqFormEntity
      entity; //extraData:[{'attachmentName':'xx'}]  extraData里面存在这个字段即可

  /// 控件不可编辑时点击回调
  final Function(Map item)? onTap;

  const KqAttachmentView({Key? key, required this.entity, this.onTap})
      : super(key: key);

  @override
  KqAttachmentViewState createState() => KqAttachmentViewState();
}

class KqAttachmentViewState extends State<KqAttachmentView> {
  Widget renderAnnex() {
    return ListView.builder(
        padding: EdgeInsets.zero,
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: widget.entity.extraData == null ||
                (widget.entity.extraData as List).isEmpty
            ? 0
            : (widget.entity.extraData as List).length,
        itemBuilder: ((context, index) {
          Map item = widget.entity.extraData[index];
          int count = (widget.entity.extraData as List).length;
          double hor = widget.entity.paddingHorizontal ??
              KqThemeManager.instance
                  .getConfig()
                  .formConfig
                  .paddingHorizontal ??
              16;
          double bottom = count == index + 1
              ? widget.entity.paddingVertical ??
                  KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingVertical ??
                  15
              : 6;
          return Container(
            padding: EdgeInsets.only(
                left: hor, right: hor, top: 6, bottom: bottom),
            child: KqUnderlineButton(
              title: item['attachmentName'],
              textAlign: TextAlign.start,
              onTap: () {
                //不能在这里查看 因为需要通过osskey去请求获取url 会在加一个文件去统一处理
                if (widget.onTap != null) {
                  widget.onTap!(item);
                }
                // FilePreviewUtil.previewFile(item['url'], item['attachmentName']);
              },
            ),
          );
        }));
  }

  //头部
  Widget renderHead() {
    double ver = widget.entity.paddingVertical ??
        KqThemeManager.instance.getConfig().formConfig.paddingVertical ??
        15;
    double hor = widget.entity.paddingHorizontal ??
        KqThemeManager.instance.getConfig().formConfig.paddingHorizontal ??
        16;
    return Container(
      padding: EdgeInsets.only(
          bottom: widget.entity.extraData == null ||
                  (widget.entity.extraData as List).isEmpty
              ? ver
              : 0,
          top: ver,
          right: hor),
      child: Row(
        children: <Widget>[
          ///是否必填
          Container(
            alignment: Alignment.centerRight,
            padding: EdgeInsets.only(
              right: 2,
            ),
            width: hor,
            child: Text(
              "*",
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontSize: KqThemeManager.instance
                          .getConfig()
                          .formConfig
                          .mustInputFontSize ??
                      14,
                  color: widget.entity.mustInput
                      ? KqThemeColors.textRed
                      : KqThemeColors.bgTransparent),
            ),
          ),
          Flexible(
              child: Text(
            widget.entity.title,
            maxLines: widget.entity.multiline ? null : 1,
            overflow: widget.entity.multiline ? null : TextOverflow.ellipsis,
            style: widget.entity.titleStyle ??
                TextStyle(
                    fontSize: KqThemeManager.instance
                            .getConfig()
                            .formConfig
                            .titleFontSize ??
                        16,
                    color: KqThemeColors.text26),
          )),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [renderHead(), renderAnnex(), const KqDivider()],
      ),
    );
  }
}
